package com.wang.auto;

import com.wang.conf.AppConf;
import com.wang.conf.AppData;
import com.wang.pojo.Device;
import com.wang.pojo.SysDictItem;
import com.wang.service.*;
import com.wang.socket.SocketMap;
import com.wang.util.DevStatusUtil;
import com.wang.util.PrintUtil;
import com.wang.util.SpringUtil;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.ParseException;
import java.util.*;
import java.util.Timer;
import java.util.concurrent.ConcurrentHashMap;


public class InitDataTask {


    /*static {
        init();
    }*/


    public static void init() {

        //自动执行任务
        //将所有socket 设备设置为离线
        new Thread(() -> {
            //setSocketDevOffLine();
        }).start();


        PrintUtil.print("启动初始化设备完成");


        Timer timer1 = new Timer();
        timer1.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                PrintUtil.print("开始获取设备列表");
                getDevList();
                PrintUtil.print("获取设备列表完成");
            }
        }, new Date(), AppConf.updateDevTime);
    }

    public static void setSocketDevOffLine() {
        DeviceService deviceService = SpringUtil.getBean("deviceServiceImpl", DeviceServiceImpl.class);
        //设置离线
        //查询所有在线设备

        List<Device> deviceList = new ArrayList<>();
         List<Device> jsonDeviceList = deviceService.selectJSONSocketDev(AppData.SocketTxCode, AppData.JSONDataCode);
        List<Device> modbusDeviceList = deviceService.selectModbusSocketDev(AppData.SocketTxCode, AppData.ModbusDataCode);
        List<Device> custom1DeviceList = deviceService.selectModbusSocketDev(AppData.SocketTxCode, AppData.CustomOneCode);
        deviceList.addAll(jsonDeviceList);
        deviceList.addAll(modbusDeviceList);
        deviceList.addAll(custom1DeviceList);
        for (Device device : deviceList) {
            if (device.getDevStatusCode().equals(AppData.onLineCode)) {
                if(!device.getDevTypeId().equals("104")){ //排除G8815
                    //执行离线事件
                    try {
                        DevStatusUtil.handerOfflineEvent(device.getDevId());
                    } catch (ParseException e) {
                        PrintUtil.print("启动初始化设备故障");
                    }
                }


            }
        }

        PrintUtil.print("初始化");


    }


    public static void getDevList() {

        if (SocketMap.deviceMap != null) {
            SocketMap.deviceMap.clear();
        }

        //获取service
        DeviceService deviceService = SpringUtil.getBean("deviceServiceImpl", DeviceServiceImpl.class);
        List<Device> jsonDeviceList = deviceService.selectJSONSocketDev(AppData.SocketTxCode, AppData.JSONDataCode);
        List<Device> modbusDeviceList = deviceService.selectModbusSocketDev(AppData.SocketTxCode, AppData.ModbusDataCode);
        List<Device> custom1DeviceList = deviceService.selectCustom1SocketDev(AppData.SocketTxCode, AppData.CustomOneCode);
        List<Device> custom2DeviceList = deviceService.selectCustom2SocketDev(AppData.SocketTxCode, AppData.CustomTWOCode);

        PrintUtil.print("socket 设备 json 共有" + jsonDeviceList.size() + "台");
        PrintUtil.print("socket 设备 modbus 共有" + modbusDeviceList.size() + "台");
        PrintUtil.print("socket 设备 custom1 共有" + custom1DeviceList.size() + "台");
        PrintUtil.print("socket 设备 custom2 共有" + custom2DeviceList.size() + "台");


        Map<String, Device> deviceMap = new HashMap<>();
        if (jsonDeviceList != null && jsonDeviceList.size() > 0) {
            for (Device device : jsonDeviceList) {
                device.setTxType(AppData.SocketTxCode);
                device.setTxDataType(AppData.JSONDataCode);
                deviceMap.put(device.getDevGId(), device);
            }

        }

        if (modbusDeviceList != null && modbusDeviceList.size() > 0) {

            for (Device device : modbusDeviceList) {
                device.setTxType(AppData.SocketTxCode);
                device.setTxDataType(AppData.ModbusDataCode);
                deviceMap.put(device.getDevGId(), device);
            }
        }
        if (custom1DeviceList != null && custom1DeviceList.size() > 0) {

            for (Device device : custom1DeviceList) {
                device.setTxType(AppData.SocketTxCode);
                device.setTxDataType(AppData.CustomOneCode);
                deviceMap.put(device.getDevGId(), device);
            }
        }
        if (custom2DeviceList != null && custom2DeviceList.size() > 0) {


            for (Device device : custom2DeviceList) {
                device.setTxType(AppData.SocketTxCode);
                device.setTxDataType(AppData.CustomTWOCode);

                deviceMap.put(device.getDevGId(), device);
            }
        }



        SocketMap.deviceMap = deviceMap;


    }


}
